#include "mainwindow.h"
#include <QApplication>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QDockWidget>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QSplitter>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QWidget>
#include <QLabel>
#include <QTabWidget>

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("CAE Design Framework"));
    resize(1200, 800);

    // 创建中心部件
    createCentralWidget();

    // 创建工程树面板
    createProjectTreeDock();

    // 创建属性面板
    createPropertyDock();

    // 创建消息通知框
    createMessageDock();

    // 创建菜单栏和工具栏
    createMenusAndToolBars();

    // 设置停靠窗口布局
    setupDockLayout();

    // 设置状态栏
    statusBar()->showMessage(tr("Ready"));
}

MainWindow::~MainWindow()
{
}

void MainWindow::createCentralWidget()
{
    // 创建带标签页的中心视图区域
    centralTabWidget = new QTabWidget(this);

    // 创建第一个视图标签
    QTextEdit* view1 = new QTextEdit(this);
    view1->setPlainText(tr("View 1 Content\nThis is the first view tab."));
    centralTabWidget->addTab(view1, tr("View 1"));

    // 创建第二个视图标签
    QTextEdit* view2 = new QTextEdit(this);
    view2->setPlainText(tr("View 2 Content\nThis is the second view tab."));
    centralTabWidget->addTab(view2, tr("View 2"));

    // 创建第三个视图标签
    QTextEdit* view3 = new QTextEdit(this);
    view3->setPlainText(tr("View 3 Content\nThis is the third view tab."));
    centralTabWidget->addTab(view3, tr("View 3"));

    // 设置为中心部件
    setCentralWidget(centralTabWidget);
}

void MainWindow::createProjectTreeDock()
{
    // 创建工程树停靠窗口
    projectDock = new QDockWidget(tr("Project Tree"), this);
    projectDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

    // 创建工程树
    projectTree = new QTreeWidget(projectDock);
    projectTree->setHeaderLabel(tr("Project"));

    // 创建设计部分
    designItem = new QTreeWidgetItem(projectTree);
    designItem->setText(0, tr("Design"));
    designItem->setExpanded(true);

    // 添加设计子项
    QTreeWidgetItem* part1 = new QTreeWidgetItem(designItem);
    part1->setText(0, tr("Part 1"));

    QTreeWidgetItem* part2 = new QTreeWidgetItem(designItem);
    part2->setText(0, tr("Part 2"));

    // 创建仿真部分
    simulationItem = new QTreeWidgetItem(projectTree);
    simulationItem->setText(0, tr("Simulation"));
    simulationItem->setExpanded(true);

    // 添加仿真子项
    QTreeWidgetItem* analysis1 = new QTreeWidgetItem(simulationItem);
    analysis1->setText(0, tr("Static Analysis"));

    QTreeWidgetItem* analysis2 = new QTreeWidgetItem(simulationItem);
    analysis2->setText(0, tr("Modal Analysis"));

    projectDock->setWidget(projectTree);
    addDockWidget(Qt::LeftDockWidgetArea, projectDock);
    this->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
}

void MainWindow::createPropertyDock()
{
    // 创建属性面板停靠窗口
    propertyDock = new QDockWidget(tr("Properties"), this);
    propertyDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

    // 创建属性显示区域
    propertyView = new QTextEdit(propertyDock);
    propertyView->setPlainText(tr("Property Panel\nDisplay selected item properties here."));

    propertyDock->setWidget(propertyView);
    addDockWidget(Qt::RightDockWidgetArea, propertyDock);
}

void MainWindow::createMessageDock()
{
    // 创建消息通知框停靠窗口
    messageDock = new QDockWidget(tr("Messages"), this);
    messageDock->setAllowedAreas(Qt::BottomDockWidgetArea);

    // 创建消息显示区域
    messageView = new QTextEdit(messageDock);
    messageView->setPlainText(tr("Message Notification Box\nSystem messages will be displayed here."));

    messageDock->setWidget(messageView);
    // 添加到底部区域
    addDockWidget(Qt::BottomDockWidgetArea, messageDock);
}

void MainWindow::setupDockLayout()
{
    // 不需要额外的布局设置
}

void MainWindow::createMenusAndToolBars()
{
    // 创建菜单栏
    QMenu* fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(tr("&New"), this, SLOT(newFile()));
    fileMenu->addAction(tr("&Open"), this, SLOT(openFile()));
    fileMenu->addAction(tr("&Save"), this, SLOT(saveFile()));
    fileMenu->addSeparator();
    fileMenu->addAction(tr("E&xit"), this, SLOT(close()));

    QMenu* editMenu = menuBar()->addMenu(tr("&Edit"));
    editMenu->addAction(tr("&Undo"), this, SLOT(undo()));
    editMenu->addAction(tr("&Redo"), this, SLOT(redo()));
    editMenu->addSeparator();
    editMenu->addAction(tr("C&ut"), this, SLOT(cut()));
    editMenu->addAction(tr("&Copy"), this, SLOT(copy()));
    editMenu->addAction(tr("&Paste"), this, SLOT(paste()));

    QMenu* viewMenu = menuBar()->addMenu(tr("&View"));
    viewMenu->addAction(projectDock->toggleViewAction());
    viewMenu->addAction(propertyDock->toggleViewAction());
    viewMenu->addAction(messageDock->toggleViewAction());

    // 创建工具栏
    QToolBar* mainToolBar = addToolBar(tr("Main"));
    mainToolBar->addAction(tr("New"), this, SLOT(newFile()));
    mainToolBar->addAction(tr("Open"), this, SLOT(openFile()));
    mainToolBar->addAction(tr("Save"), this, SLOT(saveFile()));

    // 添加视图控制工具
    mainToolBar->addSeparator();
    QAction* addTabAction = mainToolBar->addAction(tr("Add Tab"));
    connect(addTabAction, &QAction::triggered, this, &MainWindow::addNewTab);

    QAction* closeTabAction = mainToolBar->addAction(tr("Close Tab"));
    connect(closeTabAction, &QAction::triggered, this, &MainWindow::closeCurrentTab);
}

// 槽函数实现示例
void MainWindow::newFile()
{
    messageView->append(tr("New file created."));
}

void MainWindow::openFile()
{
    messageView->append(tr("File opened."));
}

void MainWindow::saveFile()
{
    messageView->append(tr("File saved."));
}

void MainWindow::undo()
{
    messageView->append(tr("Undo action performed."));
}

void MainWindow::redo()
{
    messageView->append(tr("Redo action performed."));
}

void MainWindow::cut()
{
    messageView->append(tr("Cut operation performed."));
}

void MainWindow::copy()
{
    messageView->append(tr("Copy operation performed."));
}

void MainWindow::paste()
{
    messageView->append(tr("Paste operation performed."));
}

// 新增的标签页管理槽函数
void MainWindow::addNewTab()
{
    QTextEdit* newView = new QTextEdit(this);
    newView->setPlainText(tr("New View Content\nThis is a newly added view tab."));
    int tabIndex = centralTabWidget->addTab(newView, tr("New View"));
    centralTabWidget->setCurrentIndex(tabIndex);
    messageView->append(tr("New tab added."));
}

void MainWindow::closeCurrentTab()
{
    int currentIndex = centralTabWidget->currentIndex();
    if (centralTabWidget->count() > 1) {
        centralTabWidget->removeTab(currentIndex);
        messageView->append(tr("Current tab closed."));
    }
    else {
        messageView->append(tr("Cannot close the last tab."));
    }
}